#include "menuview.h"
#include "ui_menuview.h"

#include "dialog.h"
#include <QScrollBar>

menuview::menuview(QWidget *parent)
    : QDialog(parent)
    ,A1(5),A2(5),A3(5),A4(5),A5(5),A6(5),B1(5),B2(5),B3(5),B4(5),B5(5),B6(5)
    ,ui(new Ui::menuview)
    ,a1(0),a2(0),a3(0),a4(0),a5(0),a6(0),b1(0),b2(0),b3(0),b4(0),b5(0),b6(0)
    ,total(0)
{
    ui->setupUi(this);
    for(int i=0;i<8;i++)
    {
        status[i]=0;
    }
    desk=4;
}

menuview::~menuview()
{
    delete ui;
    delete label;
}



void Dialog::menu()
{
    menuview1->show();
}

//获取每个菜品的数量

void menuview::on_spinBox_1_valueChanged(int arg1)
{
    a1=arg1;
    A1-=arg1;
}
void menuview::on_spinBox_2_valueChanged(int arg1)
{
    a2=arg1;
    A2-=arg1;
}
void menuview::on_spinBox_3_valueChanged(int arg1)
{
    a3=arg1;
    A3-=arg1;
}
void menuview::on_spinBox_4_valueChanged(int arg1)
{
    a4=arg1;
    A4-=arg1;
}
void menuview::on_spinBox_5_valueChanged(int arg1)
{
    a5=arg1;
    A5-=arg1;
}
void menuview::on_spinBox_6_valueChanged(int arg1)
{
    a6=arg1;
    A6-=arg1;
}
void menuview::on_spinBox_7_valueChanged(int arg1)
{
    b1=arg1;
    B1-=arg1;
}
void menuview::on_spinBox_8_valueChanged(int arg1)
{
    b2=arg1;
    B2-=arg1;
}
void menuview::on_spinBox_9_valueChanged(int arg1)
{
    b3=arg1;
    B3-=arg1;
}
void menuview::on_spinBox_10_valueChanged(int arg1)
{
    b4=arg1;
    B4-=arg1;
}
void menuview::on_spinBox_11_valueChanged(int arg1)
{
    b5=arg1;
    B5-=arg1;
}
void menuview::on_spinBox_12_valueChanged(int arg1)
{
    b6=arg1;
    B6-=arg1;
}

//menuview的友元函数
void getContent(menuview* s,string& content)
{
    if(s->a1)
    {
        content+="Braised Pork Belly ×"+std::to_string(s->a1)+"\n";
    }
    if(s->a2)
    {
        content+="Lobster ×"+std::to_string(s->a2)+"\n";
    }
    if(s->a3)
    {
        content+="Poached Chicken ×"+std::to_string(s->a3)+"\n";
    }
    if(s->a4)
    {
        content+="Sweet Spare Ribs ×"+std::to_string(s->a4)+"\n";
    }
    if(s->a5)
    {
        content+="Spicy Fish Hotpot ×"+std::to_string(s->a5)+"\n";
    }
    if(s->a6)
    {
        content+="Steamed Sea Bass ×"+std::to_string(s->a6)+"\n";
    }
    if(s->b1)
    {
        content+="Rice Ball ×"+std::to_string(s->b1)+"\n";
    }
    if(s->b2)
    {
        content+="Bread ×"+std::to_string(s->b2)+"\n";
    }
    if(s->b3)
    {
        content+="Pizza ×"+std::to_string(s->b3)+"\n";
    }
    if(s->b4)
    {
        content+="Bun ×"+std::to_string(s->b4)+"\n";
    }
    if(s->b5)
    {
        content+="Noodles ×"+std::to_string(s->b5)+"\n";
    }
    if(s->b6)
    {
        content+="Rice ×"+std::to_string(s->b6)+"\n";
    }
    content+="---------------------------\n";
}

void menuview::soldout()
{
    QPixmap newPixmap(":/new/prefix1/picture/SoldOut.jpg"); // 替换路径为你的图片路径

    if(A1<0)
    {
        ui->label_3->setPixmap(newPixmap);
        // 显示 QLabel
        ui->label_3->show();
        ui->spinBox_1->setEnabled(false);
    }
    if(A2<0)
    {
        ui->label_6->setPixmap(newPixmap);
        // 显示 QLabel
        ui->label_6->show();
        ui->spinBox_2->setEnabled(false);
    }
    if(A3<0)
    {
        ui->label_9->setPixmap(newPixmap);
        // 显示 QLabel
        ui->label_9->show();
        ui->spinBox_3->setEnabled(false);
    }
    if(A4<0)
    {
        ui->label_12->setPixmap(newPixmap);
        // 显示 QLabel
        ui->label_12->show();
        ui->spinBox_4->setEnabled(false);
    }
    if(A5<0)
    {
        ui->label_15->setPixmap(newPixmap);
        // 显示 QLabel
        ui->label_15->show();
        ui->spinBox_5->setEnabled(false);
    }
    if(A6<0)
    {
        ui->label_18->setPixmap(newPixmap);
        // 显示 QLabel
        ui->label_18->show();
        ui->spinBox_6->setEnabled(false);
    }

}

//提交订单
void menuview::on_pushButton_2_clicked()
{
    if(this->desknum=="")
    {
        QMessageBox msgBOX;
        msgBOX.setText("Please input your desknum!");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        total+=a1*52+a2*66+a3*40+a4*56+a5*60+a6*55
                 +b1*15+b2*19+b3*20+b4*10+b5*15+b6*2;
        ui->label_total->setText(QString::fromStdString(to_string(total)));

        //展示选购菜品的名称
        string& content=this->content;

        //没有改变的字符串--用来后方传出去增加的内容--字符串截取算法
        string oldcontent=content;

        //将选购菜品加入content
        getContent(this,content);

        //设置字体族
        QFont font("微软雅黑",10);
        label->setFont(font);
        //加入进去
        label->setText(QString::fromStdString(content));

        // 设置标签的尺寸策略，确保标签的大小适应内容
        label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

        // 将标签添加到滚动区域中
        ui->scrollArea->setWidget(label);

        // 将滚动条移动到顶部位置
        ui->scrollArea->verticalScrollBar()->setValue(ui->scrollArea->verticalScrollBar()->minimum());


        //提交订单后，更改选购菜品的数量
        ui->spinBox_1->setValue(0);
        ui->spinBox_2->setValue(0);
        ui->spinBox_3->setValue(0);
        ui->spinBox_4->setValue(0);
        ui->spinBox_5->setValue(0);
        ui->spinBox_6->setValue(0);
        ui->spinBox_7->setValue(0);
        ui->spinBox_8->setValue(0);
        ui->spinBox_9->setValue(0);
        ui->spinBox_10->setValue(0);
        ui->spinBox_11->setValue(0);
        ui->spinBox_12->setValue(0);

        //传入截取获得的字符串
        emit SubmitOrder(content.substr(oldcontent.size(),content.size()),this->desknum);

        soldout();
    }
}

//结账
void menuview::on_pushButton_3_clicked()
{
    if(this->desknum!="")
    {
        emit Pay(this->total,this->desknum);
        this->content.clear();
        this->total=0;
        this->label->setText("You have pay for this order!");
        ui->label_total->setText(QString::fromStdString(to_string(total)));
        ui->lineEdit->clear();

        this->desknum="";
    }
}


//sit down按钮
void menuview::on_pushButton_4_clicked()
{
    this->desknum=ui->lineEdit->text().toStdString();
    if(this->desknum=="")
    {
        QMessageBox msgBOX;
        msgBOX.setText("Please input your desknum!");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        if(desknum<"9"&&desknum>"0"&&0<std::stoi(desknum)&&std::stoi(desknum)<9)
        {
            if(std::stoi(desknum)>this->desk)
            {
                QMessageBox msgBOX;
                msgBOX.setText("Sorry, this table is not open");
                msgBOX.setStandardButtons(QMessageBox::Ok);
                msgBOX.exec();
            }
            else
            {
                //确保所用桌子是空闲状态
                if(this->status[std::stoi(desknum)-1]==0)
                {
                    emit sitdown(desknum);
                    this->status[std::stoi(desknum)-1]=1;

                }
                else
                {
                    QMessageBox msgBOX;
                    msgBOX.setText("Sorry,this desk is busy!");
                    msgBOX.setStandardButtons(QMessageBox::Ok);
                    msgBOX.exec();
                }
            }

        }
        else
        {
            QMessageBox msgBOX;
            msgBOX.setText("Error Desk Number");
            msgBOX.setStandardButtons(QMessageBox::Ok);
            msgBOX.exec();
        }
    }
}


//呼叫服务员
void menuview::on_pushButton_clicked()
{
    if(this->desknum=="")
    {
        QMessageBox msgBOX;
        msgBOX.setText("Please input your desknum!");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        emit Callwaiter(this->desknum);
    }
}

//更改桌子状态的槽函数
void menuview::slot_State(uint64_t desknum)
{
    this->status[desknum-1]=0;
}

void menuview::slot_Modify(int num)
{
    this->desk+=num;
}
